package com.pn.controller;

import com.pn.dto.AssignRoleDto;
import com.pn.entity.*;
import com.pn.page.Page;
import com.pn.service.RoleService;
import com.pn.service.UserRoleService;
import com.pn.service.UserService;
import com.pn.utils.TokenUtils;
import com.pn.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @Author :LY
 * @Description :
 * @Date :2025/3/27 0:35
 */
@RequestMapping("/user")
@RestController
public class UserController {

    //注入UserService
    @Autowired
    private UserService userService;

    //分页查询用户的url接口/user/user-list
    @RequestMapping("/user-list")
    public Result userList(Page page, User user){
        //执行业务
        page = userService.queryUserByPage(page,user);
        //响应
        return Result.ok(page);

    }

    /**
        添加用户的url接口/user/addUser
        参数@RequestBody User user -- 接收并封装前端传递的json数据的用户信息
     */
    //注入TokenUtils
    @Autowired
    private TokenUtils tokenUtils;

    @RequestMapping("/addUser")
    public Result addUser(@RequestBody User user, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        //拿到当前登录的用户id
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int createBy = currentUser.getUserId();
        user.setCreateBy(createBy);

        //执行业务
        Result result = userService.saveUser(user);

        //响应
        return result;
    }

    //启用或禁用用户的url接口/user/updateState
    @RequestMapping("/updateState")
    public Result updateUserState(@RequestBody User user){
        //执行业务
        Result result = userService.setUserState(user);
        //响应
        return result;
    }

    /*
     查询用户已分配角色的url接口/user/user-role-list/{userId}
     @PathVariable Integer userId --表示将路径占位符userId的值（用户id）赋值给请求处理方法入参变量userId；
     */
    //注入UserRoleService
    @Autowired
    private RoleService roleService;
    @RequestMapping("/user-role-list/{userId}")
    public Result userRoleList(@PathVariable Integer userId){
        //执行业务
       List<Role> roleIdList = roleService.queryUserRoleByUid(userId);
        //响应
        return Result.ok(roleIdList);
    }

    //给用户分配角色的url接口/user/assignRole
    @RequestMapping("/assignRole")
    public Result assignRole(@RequestBody AssignRoleDto assignRoleDto){
        //执行业务
        userService.assignRole(assignRoleDto);
        //响应
        return Result.ok("分配角色成功！");
    }

    //根据用户id删除单个用户的url接口/user/deleteUser/{userId}
    @RequestMapping("/deleteUser/{userId}")
    public Result deleteUserById(@PathVariable Integer userId){
        //执行业务
       Result result = userService.removeUserByIds(Arrays.asList(userId));
       //响应
       return result;
    }

    //根据用户ids批量删除用户的url接口/user/deleteUserList
    @RequestMapping("/deleteUserList")
    public Result deleteUserByIds(@RequestBody List<Integer> userIdList){
        //执行业务
        Result result = userService.removeUserByIds(userIdList);
        //响应
        return result;
    }

    //修改用户的url接口/user/updateUser
    @RequestMapping("/updateUser")
    public Result updateUser(@RequestBody User user,@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        //拿到当前登录的用户id
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int updateBy = currentUser.getUserId();

        user.setUpdateBy(updateBy);

        ///执行业务
        Result result = userService.setUserById(user);

        //响应
        return result;
    }

    //重置密码的url接口/user/updatePwd/{userId}
    @RequestMapping("/updatePwd/{userId}")
    public Result resetPassword(@PathVariable Integer userId){
        //执行业务
        Result result = userService.setPwdById(userId);
        //响应
        return result;
    }
}
